<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * FeedController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class FeedController extends Zend_Controller_Action
{
	private $settings; // settings
	
	/*
	 * Init settings
	 * 
	 */
	public function init()
	{		
		// settings
		$this->settings	= Zend_Registry::get('settings');
		
		Zend_Layout::getMvcInstance()->disableLayout();
	}
	
	
	
	/*
	 * Shows the latest threads (RSS)
	 * 
	 * @param null
	 * 
	 * @return string XML-Header with RSS-Data
	 */
	public function threadsAction()
	{
		// db
		$threads	= new Threads();
		$boards		= new Boards();
		$posts		= new Posts();
		
		// fetch
		$getThreads	= $threads->fetchAll('is_public = 1 AND is_deleted = 0', 'create_date DESC', 10);
		
		// var
		$baseUrl	= $this->settings['domain_name'];
		
		// helper
		$url		= new Zend_View_Helper_Url();
		$author		= new MHDEV_View_Helper_GetAuthor();
		$date		= new MHDEV_View_Helper_Datetime();
		$cleanUrl	= new MHDEV_View_Helper_CleanUrlTitle();
		
		$feedArray = array(
			'title'         => $this->settings['board_name'],
			'link'          => $baseUrl,
			'description'   => Zend_Registry::get('lang')->_('feed_threads_desc'),
			'language'      => $this->settings['default_language'],
			'charset'       => 'utf-8',
			'pubDate'       => time(),
			'generator'     => 'Zend Framework Zend_Feed',
			'entries'       => array()
		);
		
		// add posts to feed
		if($getThreads->count() > 0)
		{
			foreach($getThreads->toArray() AS $thread)
			{
				$getPost	= $posts->fetchAll('is_public = 1 AND is_deleted = 0 AND thread_uid = ' . $thread['uid'], 'create_date ASC', 1);

				if($getPost->count() > 0)
				{
					$post		= $getPost->toArray();
					
					$getBoard 	= $boards->fetchAll('is_public = 1 AND is_deleted = 0 AND group_uid <= 1 AND uid =' . $thread['board_uid']);
					if($getBoard->count() > 0)
					{
						$created	= $date->datetime($thread['create_date'], 'dd-MM-YYYY');
						$createdArr	= explode('-', $created);
						
						$feedArray['entries'][] = array(
							'title'         => $thread['title'],
							'link'          => $baseUrl . $url->url(array('controller' => 'posts', 'uid' => $thread['uid'], 'title' => $cleanUrl->CleanUrlTitle($thread['title'])), 'threads', true),
							'guid'          => $baseUrl . $url->url(array('controller' => 'posts', 'uid' => $thread['uid'], 'title' => $cleanUrl->CleanUrlTitle($thread['title'])), 'threads', true),
							'description'   => $post[0]['text'],
							'lastUpdate'    => mktime(0, 0, 0, $createdArr[1], $createdArr[0], $createdArr[2])
						);
					}
					else
					{
						continue;
					}
				}
				else
				{
					continue;
				}
			}
		}
		
		// send output to browser
		$feed = Zend_Feed::importArray($feedArray, 'rss');
		return $feed->send();
	}
	
	
	
	/*
	 * Shows the latest posts (RSS)
	 * 
	 * @param null
	 * 
	 * @return string XML-Header with RSS-Data
	 */
	public function postsAction()
	{
		// db
		$posts		= new Posts();
		$threads	= new Threads();
		$boards		= new Boards();
		
		// fetch
		$getPosts	= $posts->fetchAll('is_deleted = 0 AND is_public = 1', 'create_date DESC');
		
		// var
		$baseUrl	= $this->settings['domain_name'];
		
		// helper
		$url		= new Zend_View_Helper_Url();
		$author		= new MHDEV_View_Helper_GetAuthor();
		$date		= new MHDEV_View_Helper_Datetime();
		$cleanUrl	= new MHDEV_View_Helper_CleanUrlTitle();
		
		$feedArray = array(
			'title'         => 'Mein Feed',
			'link'          => $baseUrl,
			'description'   => 'Meine Feed-Beschreibung',
			'language'      => $this->settings['default_language'],
			'charset'       => 'utf-8',
			'pubDate'       => time(),
			'generator'     => 'Zend Framework Zend_Feed',
			'entries'       => array()
		);
		
		// add posts to feed
		if($getPosts->count() > 0)
		{
			foreach($getPosts->toArray() AS $post)
			{
				$getThread = $threads->fetchAll('is_public = 1 AND is_deleted = 0 AND uid =' . $post['thread_uid']);
				if($getThread->count() > 0)
				{
					$thread		= $getThread->toArray();
					$getBoard 	= $boards->fetchAll('is_public = 1 AND is_deleted = 0 AND group_uid <= 1 AND uid =' . $thread[0]['board_uid']);
					if($getBoard->count() > 0)
					{
						
						$created	= $date->datetime($post['create_date'], 'dd-MM-YYYY');
						$createdArr	= explode('-', $created);
						
						$feedArray['entries'][] = array(
							'title'         => $author->GetAuthor($post['user_uid']) . ' - ' . $post['title'],
							'link'          => $baseUrl . $url->url(array('controller' => 'posts', 'uid' => $thread[0]['uid'], 'title' => $cleanUrl->CleanUrlTitle($thread[0]['title'])), 'threads', true),
							'guid'          => $baseUrl . $url->url(array('controller' => 'posts', 'uid' => $thread[0]['uid'], 'title' => $cleanUrl->CleanUrlTitle($thread[0]['title'])), 'threads', true),
							'description'   => $post['text'],
							'lastUpdate'    => mktime(0, 0, 0, $createdArr[1], $createdArr[0], $createdArr[2])
						); 
					}
					else
					{
						continue;
					}
				}
				else
				{
					continue;
				}
			}
		}
		
		// send output to browser
		$feed = Zend_Feed::importArray($feedArray, 'rss');
		return $feed->send();
	}
}
?>